package com.fysports.web.controller.user;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.framework.kit.security.MD5Kit;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.cache.DictCacheKit;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.commons.StringKit;
import com.fysports.web.kit.dtgrid.DTGridKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.model.user.BizUser;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;
import com.fysports.web.service.EntityManager;

import java.util.List;
import java.util.Map;


/**
 * 基础用户信息维护 <br/>
 * Created by konbluesky<br/>
 * Date : 14-9-2 下午8:24<br/>
 * Project : JdmData_WebServer<br/>
 */
public class UserInfoController extends AbstractBaseController implements RestFulController {

    public void index() {
        moduleFactory.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("user/users_list.ftl"));
    }


    /**
     * dtgrid ajax数据抓取 1.数据初始化 2.快速查询+高级查询
     * 数据查询:
     * 高级查询->快速查询->默认加载
     */
    public void load() {
        /* 1.初始化dtview */
        DTGridView dtview = DTGridKit.toDtView(getPara(DTGridKit.PARAM_NAME));

        Page<BizUser> list;
        List<Object> args = Lists.newArrayList();
        String sqlext = " and liveflag = 1"; //TODO 筛选未逻辑删除字段
        // 高级查询
        if (DTGridKit.isAdvance(dtview)) {
            sqlext += DTGridKit.getQueryConditionByType(dtview, args, DTGridKit.TYPE_ADVANCEQUERY);
            list = BizUser.dao
                    .DTSearchPaginate(dtview.getNowPage(), dtview.getPageSize(), null, sqlext,null,args);
        }// 快速查询
        else if (DTGridKit.isFast(dtview)) {
            sqlext += DTGridKit.getQueryConditionByType(dtview, args, DTGridKit.TYPE_FASTQUERY);
            list = BizUser.dao
                    .DTSearchPaginate(dtview.getNowPage(), dtview.getPageSize(), null, sqlext,null,args);
        }// 默认加载
        else {
            Map<String, Object> params = Maps.newHashMap();
            params.put(BizUser.COL_LIVEFLAG, 1);
            list = BizUser.dao.searchPaginate(dtview.getNowPage(), dtview.getPageSize(),params);
        }
        /* 2.列表数据回写 */
        dtview = DTGridKit.toDtView(dtview, list);
        if (dtview.getIsExport()) {
            renderExport(dtview);
        }
        else {
            renderJson(dtview);
        }
    }


    @Override public void add() {
        setAttr("m", new BizUser());
        render(PathBizKit.getPageAdminPath("user/users_add.ftl"));
    }


    @Override public void edit() {
        moduleFactory.create(this);
        execute();

        String uuid = getPara();
        setAttr("m", EntityManager.getUserEntity(uuid));
//        BizUser eo = BizUser.dao.findById(uuid);
//        setAttr("m", eo);
        render(PathBizKit.getPageAdminPath("user/users_update.ftl"));
    }


    @Override public void save() {
        BizUser eo = getModel(BizUser.class, "m");
        String pwd = eo.getStr("pwd");
        // 用户密码默认从缓存中读取
        if (StringKit.isBlank(pwd)) {
            pwd = DictCacheKit.getUserDefaultPassword();
        }
        eo.set(BizUser.COL_PWD, MD5Kit.get32MD5(pwd));
        boolean status = eo.save();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);
        renderJson(json);
    }


    @Override public void update() {
        BizUser sm = getModel(BizUser.class, "m");
        boolean status = sm.update();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.UPDATE_SUCCESS : OperateType.UPDATE_FAIL);
        renderJson(json);
    }


    @Override public void delete() {
//        String uuid = getPara();
//        JsonView json = new JsonView();
//        BizUser sm = BizUser.dao.findById(uuid);
//        boolean status = false;
//        if (sm != null) {
//            sm.set(BizUser.COL_LIVEFLAG, "0");
//            status = sm.update();
//        }
//        json.setStatus(status);
//        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
//        renderJson(json);
        renderJson(new JsonView(false,OperateType.BAD_REQUEST));
    }


    public void info() {
        String uuid = getPara();
        setAttr("m", EntityManager.getUserEntity(uuid));
        render(PathBizKit.getPageAdminPath("user/users_info.ftl"));
    }

}
